package com.example.administrator.mail_list;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class EditActivity extends AppCompatActivity {
    private EditText editNum;
    private EditText editName;
    private EditText editAge;
    private RadioButton radioBtn1;
    private RadioButton radioBtn2;
    private RadioButton radioBtn3;
    private RadioButton radioBtn4;
    private RadioButton radioBtn5;
    private Button saveStu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);

        findViews();

        initViewData();

        SaveOnClick();
    }

    //编辑
    private void SaveOnClick() {
        saveStu = findViewById(R.id.btn_save);
        saveStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String number = editNum.getText().toString();
                String name = editName.getText().toString();
                int age = Integer.parseInt(editAge.getText().toString());

                int imgId = R.drawable.s1;

                if(radioBtn1.isChecked()){
                    imgId = R.drawable.s1;
                }else if(radioBtn2.isChecked()){
                    imgId = R.drawable.s2;
                }else if(radioBtn3.isChecked()){
                    imgId = R.drawable.s3;
                }else if(radioBtn4.isChecked()){
                    imgId = R.drawable.s4;
                }else if(radioBtn5.isChecked()){
                    imgId = R.drawable.s5;
                }

                Intent intent = new Intent();
                intent.putExtra("NUMBER",number);
                intent.putExtra("NAME",name);
                intent.putExtra("AGE",age);
                intent.putExtra("IMG",imgId);
                setResult(3001,intent);

                finish();
            }
        });
    }

    //获得上一个活动传递的数据
    private void initViewData() {
        Intent intent = getIntent();
        String number = intent.getStringExtra("NUMBER");
        String name = intent.getStringExtra("NAME");
        int age = intent.getIntExtra("AGE",0);
        int imgId = intent.getIntExtra("IMG",R.drawable.s1);

        //将获取的信息编辑到对应的组件中
        editNum.setText(number);
        editName.setText(name);
        editAge.setText(String.valueOf(age));

        switch (imgId){
            case R.drawable.s1:
                radioBtn1.setChecked(true);
                break;
            case R.drawable.s2:
                radioBtn2.setChecked(true);
                break;
            case R.drawable.s3:
                radioBtn3.setChecked(true);
                break;
            case R.drawable.s4:
                radioBtn4.setChecked(true);
                break;
            case R.drawable.s5:
                radioBtn5.setChecked(true);
                break;
        }
    }

    //找到各个组件
    private void findViews() {
        editNum = findViewById(R.id.editNum);
        editName = findViewById(R.id.editName);
        editAge = findViewById(R.id.editAge);

        radioBtn1 = findViewById(R.id.radioBtn1);
        radioBtn2 = findViewById(R.id.radioBtn2);
        radioBtn3 = findViewById(R.id.radioBtn3);
        radioBtn4 = findViewById(R.id.radioBtn4);
        radioBtn5 = findViewById(R.id.radioBtn5);
    }
}
